package com.fick.struts.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;






//import org.apache.log4j.Logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.fick.hibernate.dao.QuestionDAO;
import com.fick.hibernate.model.Admin;
import com.fick.hibernate.model.Question;
import com.fick.struts.form.MessageForm;
import com.fick.tools.SystemLogger;
import com.fick.util.General;

public class ManagerMessageAction extends DispatchAction {
	// private static Logger log = Logger.getLogger(MessageFeedback.class);

	private static final Log log = LogFactory.getLog(ManagerMessageAction.class);

	public ActionForward listMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SystemLogger.operatelog(request, "查询留言反馈");
		// HttpSession session = request.getSession();
		List list = new ArrayList();
		try {
			// Question question = new Question();
			QuestionDAO questionDao = new QuestionDAO();
			list = questionDao.findAll();
			request.setAttribute("msglist", list);
			log.debug("list.size = " + list.size());
			SystemLogger.operatelog(request, "list.size = " + list.size());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "留言反馈", "查询留言失败");
			return null;
		}
		return mapping.findForward("infoMessage");// listMessage
	}

	public ActionForward infoMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Question question = null;
		String id = request.getParameter("id");
		try {
			QuestionDAO questionDao = new QuestionDAO();
			question = questionDao.findById(new Integer(id));
			request.setAttribute("question", question);
			log.info("question = " + question.toString());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "留言反馈", "查询留言失败");
			return null;
		}
		return mapping.findForward("infoMessage");
	}

	public ActionForward addMessage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		MessageForm msgbackform = (MessageForm) form;
		String username = (msgbackform.getUsername());
		String body = (msgbackform.getBody());
		String home = (msgbackform.getHome());
		String phone = (msgbackform.getPhone());
		Question question = null;
		List list = null;
		try {
			question = new Question();
			question.setUserName(username);
			question.setQuestionContent(body);
			question.setUserTel(phone);
			question.setUserAddress(home);
			question.setQuestionDate(new Date());
			question.setAnswerDate(new Date());
			question.setAnswerContent("");
			question.setAdminName("");
			question.setIfanswer("0");
			// Admin admin = new Admin();
			// admin = (Admin)request.getSession().getAttribute("admin");
			// admin.getAdminName();
			QuestionDAO questionDao = new QuestionDAO();
			questionDao.save(question);
			SystemLogger.operatelog(request, "添加留言成功!");
			QuestionDAO dao = new QuestionDAO();
			list = dao.findAll();

			request.setAttribute("msglist", list);
			SystemLogger.operatelog(request, "查询留言条数!=" + list.size());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
			SystemLogger.errorlog("1", "留言反馈", "查询留言失败");
			return null;
		}
		return mapping.findForward("toaddMessage"); // listMessage(mapping,
													// form, request, response);
													// //mapping.findForward("addMessage");
	}
}
